// 歌曲数据 - 包含3首歌曲
const songs = [
{
"title": "like JENNIE",
"artist": "Jennie",
"cover": "images/JENNIE-1-1536x864.jpeg",
"file": "music/JENNIE - like JENNIE (Explicit).mp3",
"lyrics": [
{"time": 0, "text": "《like JENNIE》"},
{"time": 5, "text": "演唱：Jennie"},
{"time": 10, "text": "词：金珍妮、Tayla Parx、Amanda"},
{"time": 15, "text": "曲：金珍妮、Tayla Parx、Amanda"},
{"time": 20, "text": ""},
{"time": 25, "text": "Ha, come on, it's gon' be fuckin' hard"},
{"time": 30, "text": "Special edition and your AI couldn't copy"},
{"time": 35, "text": "I'm leavin' clues in the fittin' room and it's hot tea"},
{"time": 40, "text": "No, I'm not thinkin' 'bout no exes"},
{"time": 45, "text": "Know they miss me"},
{"time": 50, "text": "I got the whole room spinnin' like it's tipsy"},
{"time": 55, "text": "Don't bore us"},
{"time": 60, "text": "Take it to the chorus"},
{"time": 65, "text": "Who wanna rock with Jennie?"},
{"time": 70, "text": "Keep your hair done"},
{"time": 75, "text": "Nails done like Jennie"},
{"time": 80, "text": "Who else got 'em obsessed like Jennie?"},
{"time": 85, "text": "Like, like, like"},
{"time": 90, "text": "(Jennie, Jennie, Jennie, Jennie, Jennie, Jennie)"},
{"time": 95, "text": "I think I really like"},
{"time": 100, "text": "Haters, they don't really like"},
{"time": 105, "text": "(Jennie, Jennie, Jennie)"},
{"time": 110, "text": "'Cause they can never ever be"},
{"time": 115, "text": "Jennie, Jennie, Jen—"},
{"time": 120, "text": "But have you ever met"},
{"time": 125, "text": "Jennie, Jennie, Jennie, Jennie, Jennie"},
{"time": 130, "text": "It's Jennie, Jennie, Jennie, Jennie, Jennie"},
{"time": 135, "text": "But have you ever met"},
{"time": 140, "text": "But have you ever met"},
{"time": 145, "text": "얼말 줘도 못해 서커스짓"},
{"time": 150, "text": "포징한번에 만들어 mosh pit"},
{"time": 155, "text": "They can't deal with me 'cause I'm priceless"},
{"time": 160, "text": "여러 셀럽들 속에 내 DNA"},
{"time": 165, "text": "Get, get outta my way"},
{"time": 170, "text": "바비가 처키가 되기 전에"},
{"time": 175, "text": "Name, shame, blame, tryna burst my bubble"},
{"time": 180, "text": "터트려봐 그럼 더 큰 홀에서"},
{"time": 185, "text": "만나는 거야 제니를"},
{"time": 190, "text": "Keep shadin'"},
{"time": 195, "text": "예술작품엔 필요해 frame이"},
{"time": 200, "text": "I've slayed it and I grade it"},
{"time": 205, "text": "Yes, I'm guilty 잘난 게 죄니"},
{"time": 210, "text": "Who wanna rock with Jennie?"},
{"time": 215, "text": "Keep you hair done"},
{"time": 220, "text": "Nails done like Jennie"},
{"time": 225, "text": "Who else got 'em obsessed like Jennie?"},
{"time": 230, "text": "Like, like, like"},
{"time": 235, "text": "I think I really like Jennie"},
{"time": 240, "text": "Haters, they don't really like Jennie"},
{"time": 245, "text": "'Cause they can never ever be Jennie"},
{"time": 250, "text": "But have you ever met"},
{"time": 255, "text": "Jennie, Jennie, Jennie, Jennie, Jennie, Jennie, Jennie"},
{"time": 260, "text": "That's Jennie, Jennie, Jennie, Jennie, Jennie, Jennie, Jennie"},
{"time": 265, "text": "Like Jennie, Jennie, Jennie, Jennie, Jennie, Jennie, Jennie"},
{"time": 270, "text": "Rock with Jennie"},
{"time": 275, "text": "Keep your hair done"},
{"time": 280, "text": "Nails done like Jennie"},
{"time": 285, "text": "Who else got 'em obsessed like"},
{"time": 290, "text": "But have you ever met Jennie?"},
{"time": 295, "text": "But have you ever met"},
{"time": 300, "text": "Jennie, Jennie, Jennie"},
{"time": 305, "text": "It's Jennie, Jennie, Jennie, Jennie"}
]
},
{
"title": "Twin",
"artist": "Jennie",
"cover": "images/JENNIE-1-1536x864.jpeg",
"file": "music/JENNIE - twin.mp3",
"lyrics": [
{"time": 0, "text": "《Twin》"},
{"time": 5, "text": "演唱：Jennie"},
{"time": 10, "text": "词：Jennie、Badriia “Bibi” Bourelly、Christopher Gage Newlin、Michael Len Williams II"},
{"time": 15, "text": "曲：Jennie、Badriia “Bibi” Bourelly、Christopher Gage Newlin、Michael Len Williams II"},
{"time": 20, "text": ""},
{"time": 25, "text": "It's like I'm writing a letter"},
{"time": 30, "text": "And I put in a 12 - ounce bottle of Heineken"},
{"time": 35, "text": "In the Atlantic on a whim"},
{"time": 40, "text": "Cause I know I've been gone too long"},
{"time": 45, "text": "But I'm writing a song"},
{"time": 50, "text": "It's like I'm writing a letter, but I'm writing a song"},
{"time": 55, "text": "It's like I'm writing a letter, but I'm writing a song"},
{"time": 60, "text": "It's like I'm writing a letter, but I'm writing a song"},
{"time": 65, "text": "Can you just bear with me?"},
{"time": 70, "text": "We were ten years in"},
{"time": 75, "text": "And young and dumb and innocent, my friend"},
{"time": 80, "text": "But I knew all along that we were both wrong"},
{"time": 85, "text": "It's like I'm writing a letter, but I'm writing a song"},
{"time": 90, "text": "It's like I'm writing a letter, but I'm writing a song"},
{"time": 95, "text": "It's like I'm writing a letter, but I'm writing a song"},
{"time": 100, "text": "I didn't leave ya, I still see ya"},
{"time": 105, "text": "When I'm bumping Ashanti"},
{"time": 110, "text": "Yeah, on the beach, yeah"},
{"time": 115, "text": "I didn't hold ya, but I still know ya"},
{"time": 120, "text": "We will make up, make things right when we get older, friend"},
{"time": 125, "text": "Twin, twin, twin"},
{"time": 130, "text": "You and I, we drifted apart"},
{"time": 135, "text": "Twin, twin, twin"},
{"time": 140, "text": "Like the white clouds under the stars"},
{"time": 145, "text": "Do you feel a way?"},
{"time": 150, "text": "I blamed the timing, wasn't timing"},
{"time": 155, "text": "We was fighting and colliding, man"},
{"time": 160, "text": "I just couldn't stay"},
{"time": 165, "text": "And I keep your name so, so, so tightly to my heartbeat"},
{"time": 170, "text": "Guess I lost you and you lost me"},
{"time": 175, "text": "But I put fuel to your flame"},
{"time": 180, "text": "I didn't leave ya, I still see ya"},
{"time": 185, "text": "When I'm bumping Ashanti"},
{"time": 190, "text": "Yeah, on the beach, yeah"},
{"time": 195, "text": "I didn't hold ya, but I still know ya"},
{"time": 200, "text": "We will make up, make things right when we get older, friend"},
{"time": 205, "text": "Twin, twin, twin"},
{"time": 210, "text": "You and I, we drifted apart"},
{"time": 215, "text": "Twin, twin, twin"},
{"time": 220, "text": "Like the white clouds under the stars"},
{"time": 225, "text": "Twin, twin, twin"},
{"time": 230, "text": "You and I, we drifted apart"},
{"time": 235, "text": "Twin, twin, twin"}
]
},

];

// 播放器初始化
const audioPlayer = document.getElementById('audio-player');
const playBtn = document.getElementById('play-btn');
const prevBtn = document.getElementById('prev-btn');
const nextBtn = document.getElementById('next-btn');
const progress = document.getElementById('progress');
const currentTimeEl = document.getElementById('current-time');
const durationEl = document.getElementById('duration');
const volumeControl = document.getElementById('volume');
const playlistEl = document.getElementById('playlist');
const lyricsEl = document.getElementById('lyrics');
const searchEl = document.getElementById('search');
const coverEl = document.getElementById('cover');
const songTitleEl = document.getElementById('song-title');
const artistEl = document.getElementById('artist');

let currentSongIndex = 0;
let isPlaying = false;
let isDraggingProgress = false;

// 加载歌曲
function loadSong(index) {
    const song = songs[index];
    audioPlayer.src = song.file;
    coverEl.src = song.cover;
    songTitleEl.textContent = song.title;
    artistEl.textContent = song.artist;
    
    updatePlaylistHighlight(index);
    displayLyrics(song.lyrics);
    
    if (isPlaying) {
        audioPlayer.play().catch(e => console.log("自动播放被阻止:", e));
    }
}

// 显示歌词
function displayLyrics(lyrics) {
    lyricsEl.innerHTML = '';
    lyrics.forEach(line => {
        const p = document.createElement('p');
        p.textContent = line.text;
        p.setAttribute('data-time', line.time);
        lyricsEl.appendChild(p);
    });
}

// 更新当前播放的歌词
function updateLyrics() {
    if (isDraggingProgress) return;
    
    const currentTime = audioPlayer.currentTime;
    const lines = lyricsEl.querySelectorAll('p');
    
    let currentLine = null;
    
    lines.forEach(line => {
        line.classList.remove('current');
        const lineTime = parseFloat(line.getAttribute('data-time'));
        
        if (lineTime <= currentTime) {
            currentLine = line;
        }
    });
    
    if (currentLine) {
        currentLine.classList.add('current');
        currentLine.scrollIntoView({
            behavior: 'smooth',
            block: 'center'
        });
    }
}

// 更新播放列表高亮
function updatePlaylistHighlight(index) {
    const items = playlistEl.querySelectorAll('li');
    items.forEach((item, i) => {
        item.classList.toggle('playing', i === index);
    });
}

// 播放歌曲
function playSong() {
    isPlaying = true;
    playBtn.textContent = '暂停';
    audioPlayer.play().catch(e => {
        console.log("播放被阻止:", e);
        // 如果自动播放被阻止，显示播放按钮
        isPlaying = false;
        playBtn.textContent = '播放';
    });
}

// 暂停歌曲
function pauseSong() {
    isPlaying = false;
    playBtn.textContent = '播放';
    audioPlayer.pause();
}

// 上一曲
function prevSong() {
    currentSongIndex--;
    if (currentSongIndex < 0) {
        currentSongIndex = songs.length - 1;
    }
    loadSong(currentSongIndex);
}

// 下一曲
function nextSong() {
    currentSongIndex++;
    if (currentSongIndex >= songs.length) {
        currentSongIndex = 0;
    }
    loadSong(currentSongIndex);
}

// 更新进度条
function updateProgress() {
    if (isDraggingProgress) return;
    
    const { currentTime, duration } = audioPlayer;
    const progressPercent = (currentTime / duration) * 100;
    progress.value = progressPercent;
    
    currentTimeEl.textContent = formatTime(currentTime);
    if (duration) {
        durationEl.textContent = formatTime(duration);
    }
    
    updateLyrics();
}

// 设置进度
function setProgress() {
    const duration = audioPlayer.duration;
    audioPlayer.currentTime = (progress.value / 100) * duration;
}

// 设置音量
function setVolume() {
    audioPlayer.volume = volumeControl.value;
}

// 格式化时间
function formatTime(seconds) {
    const mins = Math.floor(seconds / 60);
    const secs = Math.floor(seconds % 60);
    return `${mins}:${secs < 10 ? '0' : ''}${secs}`;
}

// 初始化播放列表
function initPlaylist() {
    playlistEl.innerHTML = '';
    songs.forEach((song, index) => {
        const li = document.createElement('li');
        li.textContent = `${song.title} - ${song.artist}`;
        li.addEventListener('click', () => {
            currentSongIndex = index;
            loadSong(currentSongIndex);
            playSong();
        });
        playlistEl.appendChild(li);
    });
}

// 搜索功能
function searchSongs() {
    const term = searchEl.value.toLowerCase();
    const items = playlistEl.querySelectorAll('li');
    
    items.forEach((item, index) => {
        const song = songs[index];
        const text = `${song.title} ${song.artist}`.toLowerCase();
        if (text.includes(term)) {
            item.style.display = 'block';
        } else {
            item.style.display = 'none';
        }
    });
}

// 事件监听
playBtn.addEventListener('click', () => {
    isPlaying ? pauseSong() : playSong();
});

prevBtn.addEventListener('click', prevSong);
nextBtn.addEventListener('click', nextSong);

audioPlayer.addEventListener('timeupdate', updateProgress);
audioPlayer.addEventListener('ended', nextSong);
audioPlayer.addEventListener('loadedmetadata', updateProgress);

progress.addEventListener('input', () => {
    isDraggingProgress = true;
    const duration = audioPlayer.duration || 0;
    currentTimeEl.textContent = formatTime((progress.value / 100) * duration);
});

progress.addEventListener('change', () => {
    isDraggingProgress = false;
    setProgress();
});

volumeControl.addEventListener('input', setVolume);

searchEl.addEventListener('input', searchSongs);

// 初始化
loadSong(currentSongIndex);
initPlaylist();